package com.winai.launcher.view.set;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ks.projectbasictools.utils.LogUtils;
import com.ks.projectbasictools.utils.ToastKs;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.utils.VersionUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author kangshao
 * @date 2020/10/9
 * @desc 版本号详情
 */
public class VersionDetailActivity extends BaseActivity {

    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.tv_sysV)
    TextView tvSysV;
    @BindView(R.id.tv_launV)
    TextView tvLaunV;
    @BindView(R.id.tv_aysV)
    TextView tvAysV;
    @BindView(R.id.tv_aiV)
    TextView tvAiV;

    private Context mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version_detail);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        initView();
    }

    private void initObject() {
        strTitle.setText("版本号详情");
        /*设置特殊黑体*/
        Typeface fontFace = Typeface.createFromAsset(mContext.getAssets(), "HanSansCN.otf");
        tvSysV.setTypeface(fontFace);
        tvLaunV.setTypeface(fontFace);
        tvAysV.setTypeface(fontFace);
        tvAiV.setTypeface(fontFace);
    }

    private void initView() {
        tvSysV.setText(VersionUtils.getSystemVersion());
        tvLaunV.setText(VersionUtils.getLauncherVersion(mContext));
        String aiVersion = VersionUtils.getAiVersion(mContext);
        if (aiVersion != null) {
            tvAiV.setText(aiVersion);
        } else {
            setAiVersion();//设置语音版本号
        }
        VersionUtils.getAiyishengVersionName(mContext);
        setAysVersion();//设置艾益生版本号
    }

    int aysCount = 0;

    /**
     * @date 2020/10/12
     * @desc 设置应用版本号
     */
    @SuppressLint("SetTextI18n")
    private void setAysVersion() {
        new Thread(() -> {
            DB_Common dbCommon = new DB_Common(mContext);
            String aiyishengVersion = dbCommon.get("aiyishengVersion");
            while (TextUtils.isEmpty(aiyishengVersion) && aysCount < 5) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                aiyishengVersion = dbCommon.get("aiyishengVersion");
                aysCount++;
                LogUtils.d("第" + aysCount + "次获取艾益生版本号");
                new Handler(Looper.getMainLooper()).post(() -> tvAysV.setText("第" + aysCount + "次获取应用版本号"));
            }
            String finalAv = aiyishengVersion;
            if (finalAv != null) {
                new Handler(Looper.getMainLooper()).post(() -> tvAysV.setText(finalAv));
            } else {
                LogUtils.e("获取AysVersion失败");
                new Handler(Looper.getMainLooper()).post(() -> tvAysV.setText("获取应用版本号失败"));
            }
        }).start();
    }

    int count = 0;

    /**
     * @date 2020/10/10
     * @desc 设置aiVersion
     */
    @SuppressLint("SetTextI18n")
    private void setAiVersion() {
        new Thread(() -> {
            String av = VersionUtils.getAiVersion(mContext);
            while (av == null && count < 5) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                av = VersionUtils.getAiVersion(mContext);
                count++;
                LogUtils.d("第" + count + "次获取语音版本号");
                new Handler(Looper.getMainLooper()).post(() -> tvAiV.setText("第" + count + "次获取语音版本号"));
            }
            String finalAv = av;
            if (finalAv != null) {
                new Handler(Looper.getMainLooper()).post(() -> tvAiV.setText(finalAv));
            } else {
                LogUtils.e("获取AiVersion失败");
                new Handler(Looper.getMainLooper()).post(() -> tvAiV.setText("获取语音版本号失败"));
            }
        }).start();
    }

    final static int COUNTS = 4;// 点击次数
    final static long DURATION = 1000;// 规定有效时间
    long[] mHits = new long[COUNTS];

    @OnClick({R.id.iv_back, R.id.iv_home,R.id.openADB})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(intent);
                break;
            case R.id.openADB:
                //每次点击时，数组向前移动一位
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                //为数组最后一位赋值
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();
                if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
                    mHits = new long[COUNTS];//重新初始化数组
                    boolean enableAdb = (Settings.Secure.getInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0) > 0);
                    LogUtils.d("enableAdb = " + enableAdb);
                    if (!enableAdb) {
                        Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 1);
                        LogUtils.d("已打开USB调试");
                        ToastKs.show(mContext, "已打开USB调试");
                    }
                }
                break;
        }
    }
}